<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Auth extends MY_Controller {
	
	
	public function __construct(){
	   parent::__construct();
       $this->load->model('auth_model');
	   $this->load->library('encrypt');     
	}
    public function index()
	{
		//debug($this->LoginData);
        
        if($this->LoginData){           
            redirect(DEFAULT_CONTROLLER);
        }else{
			$sobicCookie = $this->cookie->userdata('sobicCookie');
			if($sobicCookie){
				$data['loginSaveData'] = json_decode($this->encrypt->decode($sobicCookie));
			}
            $this->template->title(__("Đăng nhập"));
			$this->loginView('login_form',$data);
		
		}
        
        
	}
    
    function forgot(){ 
        $this->template->title(__("Quên mật khẩu"));
        $this->loginView('forgot');        
    }
	
	
    
    function check(){
       
    		$username = $this->input->post('username');
    		$password = $this->input->post('password');
			$autologin = $this->input->post('autologin');
			
			//Duy tri dang nhap
			 if($autologin){
				$this->cookie->set_userdata('sobicCookie',$this->encrypt->encode(json_encode(array('username'=>$username,'password'=>$password))));
			 }else{
				 $this->cookie->unset_userdata('sobicCookie');
			 }
            /* Encryption password*/   
            $embasePassword = $password.SALT;
            $embasePassword = md5($embasePassword);
            
            $objMember = $this->auth_model->getMember($username);
            //debug($objMember);
            $message = "";
            $check = -1;
            if(empty($objMember)){
                $message = __("Tài khoản không tồn tại");
                $check = -1;   
            }else{
                //check password
                if($objMember->password == $embasePassword)
                {
                    //check status
                    if($objMember->memberStatus == 1)
                    {
                        $check = 1;
                        $message = __("Đăng nhập thành công");
                        //Set SESSION cho user                
                         $this->session->set_userdata('LoginData',$objMember); 
						 
						  
                    }else{
                        $check = 0;
                        $message = __("Tài khoản chưa được kích hoạt");   
                    }
                }else{
                    $check = 0;
                    $message = __("Sai mật khẩu");
                }
            }
            
            $return = array(
            'check'=>$check,
            'message'=>$message,
            );
            echo json_encode($return);
                
    }
    
   
    function logout(){
        $this->session->unset_userdata('LoginData');
        redirect('auth');
    }
    
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */